/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vigas.calculos;


/**
 *
 * @author Gabriel
 */
public class VigaDuploEngaste implements DiagramaEsforco{

   
    double E;			// modulus of elasiticiy
    double I;			// moment of intertia
    double L;			// span length
    int numPoints;		// number of points at which to compute values
    int i;
    double rr;			// reaction at right (fixed) support
    double mr;			// moment at right (fixed) supprot

    // arrays to store distance, shear, moment and deflection
    public double x[];		// distance from the left end of the beam
    public double v[];		// shear force
    public double m[];		// moment
    public double y[];		// deflection
    public double rr1;			// reaction at left fixed end
    public double rr2;			// reaction at right fixed end
    public double mm1;			// moment at left end
    public double mm2;			// moment at right end

    public  VigaDuploEngaste(double E,double I,double L,int n)
    {
        this.L = L;
        this.E = E;
        this.I = I;
        this.numPoints=n;
        x = new double[n];
        inicializarVetores(numPoints);
        for (i=0;i<numPoints;++i)
                x[i]=(double)i*L/((double)numPoints-1.);

    }

    private void inicializarVetores(int n){
				// distance from the left end of the beam
        v = new double[n];				// shear force
        m = new double[n];				// moment
        y = new double[n];

    }


    public void calcularEsforcosCargaDistribuida(double w1, double w2, double a, double b)
    {
        // compute reaction
    	rr = w1*(b-a);

        mr = 0.5*rr*(a+b);
        inicializarVetores(numPoints);
		   // compute shear, moemnt and deflection

    	for (i=0;i<numPoints;++i)
	    {

		    if (x[i]<(L-b))
    		{
	    		v[i] = 0;

    	    	m[i] = 0;

		    	y[i] = -rr/(24*E*I)*(4*(a*a+a*b+b*b)*(L-x[i])-a*a*a-a*b*b-a*a*b-b*b*b);
			}
    		else
	    	{
		    	if (x[i]<(L-a))
			   	{
			    	v[i] = -rr/(b-a)*(x[i]-L+b);

	    			m[i] = -0.5*rr/(b-a)*Math.pow((x[i]-L+b),2);

	    			y[i] = -rr/(24*E*I)* ( 6*(a+b)*Math.pow(L-x[i],2) -4*Math.pow(L-x[i],3)
		    				+Math.pow(L-x[i]-a,4)/(b-a));
				}
				else
				{
					v[i] = -rr;

					m[i] = -0.5*rr*(2*x[i]-2*L+a+b);

					y[i] = -rr/(12*E*I)*(3*(a+b)*Math.pow(L-x[i],2)-2*Math.pow(L-x[i],3));
				}
			}
		}

        if (w2-w1!=0.)
        {
            double W = 0.5*(w2-w1)*(b-a);

    		rr += W;

	    	mr += 0.5*W*(2*a+b);

		    // compute shear, moemnt and deflection

    		for (i=0;i<numPoints;++i)
	    	{

		    	if (x[i]<(L-b))
			    {
				    v[i] += 0;

    				m[i] += 0;

	    			y[i] += -W/(60*E*I)*((5*b*b+10*a*b+15*a*a)*(L-x[i])
		    			-b*b*b-2*b*b*a-3*b*a*a-4*a*a*a);
			    }
			    else
			    {
				    if (x[i]<(L-a))
				    {
					    v[i] += -W*Math.pow(x[i]-L+b,2)/Math.pow(b-a,2);

					    m[i] += -W/3*(Math.pow(x[i]-L+b,3)/Math.pow(b-a,2));

    					y[i] += -W/(60*E*I)*((20*a+10*b)*Math.pow(L-x[i],2)-10*Math.pow(L-x[i],3)
	    					+5*Math.pow(L-x[i]-a,4)/(b-a)-Math.pow(L-x[i]-a,5)/Math.pow(b-a,2));
				    }
    				else
	    			{
		    			v[i] += -W;

			    		m[i] += -W/3*(-3*L+3*x[i]+b+2*a);

				    	y[i] += -W/(6*E*I)*((2*a+b)*Math.pow(L-x[i],2)-Math.pow(L-x[i],3));
				    }
			    }
			}
		}
    }

    public void calcularEsforcosCargaConcentrada(double W, double a) {
        // compute reaction
        double b=L-a;

        rr1 = W*b*b/(L*L*L)*(3*a+b);
        rr2 = W*a*a/(L*L*L)*(3*b+a);
        mm1 = W*a*b*b/(L*L);
        mm2 = W*a*a*b/(L*L);
        inicializarVetores(numPoints);
        // compute shear, moemnt and deflection
        for (i=0;i<numPoints;++i)
        {
            if (x[i]<a)
            {
                v[i] = rr1;
                m[i] = W*a*b*b/L/L+rr1*x[i];
                y[i] = W*b*b*x[i]*x[i]/(6*E*I*L*L*L)*(3*a*x[i]+b*x[i]-3*a*L);
            }
            else
            {
                v[i] = rr1-W;
                m[i] = W*a*b*b/L/L+rr1*x[i]-W*(x[i]-a);
                y[i] = W*a*a*Math.pow(L-x[i],2)/(6*E*I*L*L*L) *( (3*b+a)*(L-x[i])-3*b*L );
            }
        }
    }

    public void calcularEsforcosMomentoConcentrado(double mo, double a)
    {
        // compute reaction
        rr1 = -6.*mo/L/L/L*(a*L-a*a);
        rr2 = -rr1;

        mm1 = -mo/L/L*(4.*a*L-3.*a*a-L*L);
        mm2 = mo/L/L*(2.*L*a-.3*a*a);
        inicializarVetores(numPoints);
        // compute shear, moemnt and deflection till the point a
        for (i=0;i<numPoints;++i)
        {

            if (x[i]<a)
            {
                v[i] = rr1;
                m[i] = -mm1+rr1*x[i];
                y[i] = -1./6./E/I*( 3.*mm1*x[i]*x[i]-rr1*Math.pow(x[i],3.) );
            }
            else
            {
                v[i] = rr1;
                m[i] = -mm1+rr1*x[i]+mo;
                y[i] = 1./6./E/I*( (mo-mm1)*(3.*x[i]*x[i]-6.*L*x[i]+3.*L*L) - rr1*(3.*L*L*x[i]-Math.pow(x[i],3.)-2.*L*L*L));
            }
        }
    }

     public double[] getM() {
        return m;
    }

    public double[] getV() {
        return v;
    }

}
